package com.llsydn.demo.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

/**
 * @author lilinshen
 * @title 实现黑名单短信的拦截
 * @description 请填写相关描述
 * @date 2018/9/30 11:32
 */
public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //得到intent短信数据，并封装为短信对象smsMessage
        Bundle bundle = intent.getExtras();
        Object[] pdus = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);

        //取出号码
        String number = smsMessage.getOriginatingAddress();
        String content = smsMessage.getMessageBody();
        Log.e("TAG", number + "--" + content);

        //判断是否是黑名单号
        if ("110".equals(number)) {
            //如果是，中断广播（拦截短信）
            abortBroadcast();
            Log.e("TAG", "拦截到短信");
        }
    }
}
